package org.jeecg.chase.common.engine.service;

import org.jeecg.chase.common.base.data.BaseQuery;
import org.jeecg.chase.common.supper.process.CommonProcess;
import org.jeecg.chase.common.supper.query.ListQueryContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author leikai
 * @version 1.0
 * @description:
 * @date 2021/07/23/0023 17:24
 */
@Transactional
public abstract class AbstractDataEngineSurport {

    @Autowired
    protected ProcessService processService;

    protected Object executeQuery(BaseQuery query,
                                  QueryExecuteStrategy queryExecuteStrategy,
                                  ListFetchStrategy listFetchStrategy) {

        //对query进行前置处理
        List<CommonProcess> querybeforeProcessList = ListQueryContainer.getQueryBeforeProcessList(query.getModelId());
        processService.process(query, querybeforeProcessList);

        Class<?> resultClass = getResultClass(query);

        //执行query查询，得到查询结果queryResult
        Object queryResult = queryExecuteStrategy.executeQuery(query, resultClass);

        //从查询结果queryResult中提取结果list
        List<?> objList = listFetchStrategy.fetchResultList(queryResult);

        //对查询结果进行后置处理
        List<CommonProcess> resultAfterProcessList = ListQueryContainer.getResultAfterProcessList(query.getModelId());
        processService.process(objList, resultAfterProcessList);

        return queryResult;
    }

    public abstract Class<?> getResultClass(BaseQuery query);

    protected interface QueryExecuteStrategy {
        Object executeQuery(BaseQuery query, Class<?> resultClass);
    }

    protected interface ListFetchStrategy {
        List fetchResultList(Object obj);
    }
}
